package Config

import (
	"fmt"

	"io/ioutil"

	"gopkg.in/yaml.v2"
)

// 全局配置结构体
type GlobalConfig struct {
	Ginfox struct {
		Server struct {
			Port string `yaml:"port"`
		} `yaml:"server"`
	} `yaml:"ginfox"`
	Db struct {
		Name     string `yaml:"name"`
		Host     string `yaml:"host"`
		Username string `yaml:"username"`
		Password string `yaml:"password"`
		Port     string `yaml:"port"`
		Prefix   string `yaml:"prefix"`
	} `yaml:"db"`
	Redis struct {
		Host     string `yaml:"host"`
		Port     int    `yaml:"port"`
		Password string `yaml:"password"`
	} `yaml:"redis"`
	CustomRouter []struct {
		ControllerPath string `yaml:"controller_path"`
		UrlPath        string `yaml:"url_path"`
	} `yaml:"custom_router"`
	Pager struct {
		Size int `yaml:"size"`
	} `yaml:"pager"`
}

func InitConfig() *GlobalConfig {
	c := &GlobalConfig{}
	data, err := ioutil.ReadFile("./gin-fox/config/config.yml")
	if err != nil {
		fmt.Println("[ginfox]读取配置文件错误:", err)
	}
	err = yaml.Unmarshal(data, c)
	if err != nil {
		fmt.Println("[ginfox]读取配置文件错误:", err)
	}
	return c

}
